<link rel="stylesheet" href="css/chosen.css" />
<script src="js/chosen.jquery.js" type="text/javascript"></script>
        <script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_jual").datepicker({
					dateFormat  : "yy-mm-dd",        
					  changeMonth : true,
					  changeYear  : true					
				});
				
				$("#id_customer").change(function(){
					$.ajax({						
						url:'getAlamat.php',
						dataType: "html",
						type:'POST',
						data:'id_customer='+$(this).val(),
						success: function(data) {
							var obj = $.parseJSON(data);						
							if(data=="[]"){
								$("#alamat").val('');
										
							}else{							
								$("#alamat").val(obj[0].alamat);								
							}
						}
					})
				})
				/*$("#jumlah_rts").change(function(){
					$.ajax({						
						url:'cekRts.php',
						dataType: "html",
						type:'POST',
						data:'jumlah_rts='+$(this).val(),
						success: function(data) {
							//var obj = $.parseJSON(data);						
							if(data=='OK')                  
							  {
								  $('#imgTry').html('OK, Nilai Mencukupi!!!');
							  }
							  else
							  {
								  $('#imgTry').html(' Nilai Tidak Mencukupi!!!');
							  }
						}
					})
				})
				
				$('#jumlah_rts').keyup(function(){
					var jumlah = $('#jumlah').val();
					var jumlah_reject = $('#jumlah_reject').val();	
					var jumlah_rts = $(this).val();	

					var total = parseInt(jumlah_rts)+parseInt(jumlah_reject);
					//alert(total)
					if(parseInt(total)!=parseInt(jumlah)){
						alert('salah');	
					}
				});
				
				$('#jumlah_reject').keyup(function(){
					var jumlah = $('#jumlah').val();
					var jumlah_reject = $(this).val();	
					var jumlah_rts = $('#jumlah_rts').val();	
					
					var total = parseInt(jumlah_reject)+parseInt(jumlah_rts);
					//alert(total)
					if(parseInt(total)!=parseInt(jumlah)){
						alert('salah');	
					}
				})
				
				$('#jumlah').keyup(function(){
					var jumlah = $(this).val();
					var jumlah_reject = $('#jumlah_reject').val();	
					var jumlah_rts = $('#jumlah_rts').val();	
					//alert(parseInt(jumlah_reject))
					var total = parseInt(jumlah_rts)+parseInt(jumlah_reject);
					if ((jumlah_reject!='') && (jumlah_rts!='')){
						if(parseInt(total)!=parseInt(jumlah)){
							alert('salah');	
						}
					}
				})*/
				
				
			});
		</script>
<?php
if($_SESSION['bkeluar']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Barang keluar RTU </h5>								
						<div class="search global-nav">
						  <form method="GET" class="form-search js-search-form" action="index.php?pages=brg_kluar_rtu" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="brg_kluar_rtu">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="tgl_jual" <?php echo ($field=="tgl_jual")?"selected":"";?>>Tgl Jual</option>
									<option value="nota" <?php echo ($field=="nota")?"selected":"";?>>Nota</option>
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="center">Customer</th>
									<th class="center">Tgl Jual</th>
                                	<!--<th class="center">Nota</th>-->
									<th class="center">Jumlah</th>
                                  	<th class="center">Satuan</th>
									<th class="action" colspan="4"><a href="?pages=brg_kluar_rtu&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlSup1 = mysql_query("SELECT * FROM tb_penjualan_rtu $addQuery ORDER BY id_penjualan_rtu DESC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataSup = mysql_fetch_array($sqlSup1)){								

								$status = ($dataSup['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
                                     <?php 
										  $sql=mysql_query("select * from tb_customer where id_customer='$dataSup[id_customer]'");
										  $sql2=mysql_fetch_array($sql);
									?>
									<td><?php echo $sql2['nama'];?></td>
									<td class="center"><?php echo $dataSup['tgl_jual'];?></td>
									<!--<td class="center"><?php echo $dataSup['nota'];?></td>-->
                                    <?php 
										  $sql=mysql_query("select * from tb_sales where id_sales='$dataSup[id_sales]'");
										  $sql2=mysql_fetch_array($sql);
									?>
                                    <td class="center"><?php echo $dataSup['jumlah'];?></td>
                                    <td class="center"><?php echo $dataSup['satuan'];?></td>	
									<td class="action"><a href="?pages=brg_kluar_rtu&act=edit&id=<?php echo $dataSup['id_penjualan_rtu'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=brg_kluar_rtu&act=del&id=<?php echo $dataSup['id_penjualan_rtu'];?>';" class="icon-trash"></a>									</td>
										<td class="action"><a target="_blank" href="cetak_rtu.php?id=<?php echo $dataSup['id_penjualan_rtu'];?>" class="icon-print" title="Print Nota"></a></td>
										<!--<td class="action"><a href="#" class="icon-shopping-cart" title="Print Surat Jalan"></a></td>							-->
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_penjualan_rtu $addQuery ORDER BY id_penjualan_rtu DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=brg_kluar_rtu&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_penjualan_rtu = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_penjualan_rtu WHERE id_penjualan_rtu=$id_penjualan_rtu");
	$dataSup = mysql_fetch_array($sqlSup);

	if(!empty($id_penjualan_rtu)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Barang keluar</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Barang keluar</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=brg_kluar_rtu&act=update">
		<input type="hidden" name="id_penjualan_rtu" value="<?php echo $dataSup['id_penjualan_rtu'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Customer</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_customer" id="id_customer" data-placeholder=" Pilih Nama Customer" required>
                      <option ></option>
                      
                      <?php
                            $select_jur = mysql_query("SELECT * FROM tb_customer");
                            while($select_jur2 = mysql_fetch_array($select_jur)){
							 if($dataSup['id_customer']==$select_jur2['id_customer']){
						  	$selected = " selected ";
						  }else{
						  	$selected = "";
						  }
                      ?>
                      <option <?php echo $selected;?> value="<?php echo"$select_jur2[id_customer]";?>"><?php echo"$select_jur2[nama]";?></option>
                      <?php
                            }
                      ?>
                </select>			   	</tr>	
                <tr>
					<td><label>Alamat</label></td>
					<td>:</td>
					<td><input type="text" value="<?php echo $dataSup['alamat'];?>" size="30" name="alamat" id="alamat" placeholder="Alamat Customer" readonly="true"></td>
			   	</tr>
                <tr>
                  <td><label>Tgl Jual</label></td>
					<td>:</td>
				 	<td><input type="text"  value="<?php echo $dataSup['tgl_jual'];?>" size="30" name="tgl_jual" id="tgl_jual" placeholder="Tgl. Penjualan" required/></td> 
			   	</tr>		   	
			   	

                <tr>
					<td><label>Jumlah</label></td>
					<td>:</td>
			 	  <td><input type="text" value="<?php echo $dataSup['jumlah'];?>" size="30" name="jumlah" id="jumlah" placeholder="Jumlah Penjualan" required/>
			 	    <input type="hidden" value="<?php echo $dataSup['jumlah'];?>" size="30" name="jumlah_tmp" id="jumlah_tmp" placeholder="Jumlah Penjualan" /></td> 
			   	</tr>
                

                  
                   <tr>
					<td><label>Satuan</label></td>
					<td>:</td>
				 	<td><input type="text" value="Karton" size="30" name="satuan" id="satuan"  readonly="true" required/></td> 
			   	</tr>
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>
						<?php
						$sqlRtu = mysql_query("SELECT stock_rtu FROM tb_temp_rts LIMIT 1");
						$getRtu = mysql_fetch_array($sqlRtu);
						?>
						<p>&nbsp;</p>
						<p style="font:bold 14px Tahoma"><i>Total Stock Ready to Use : <?php echo $getRtu['stock_rtu'];?> Karton</i></p>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_penjualan_rtu = (int) $_POST['id_penjualan_rtu'];
	$id_customer = $_POST['id_customer'];
	$tgl_jual = $_POST['tgl_jual'];
	$nota = $_POST['nota'];
	$jumlah = $_POST['jumlah'];
	$jumlah_tmp = $_POST['jumlah_tmp'];
	$satuan = $_POST['satuan'];

if(!empty($id_penjualan_rtu)){
			//recount
			$sql_rtu=mysql_query("SELECT stock_rtu from tb_temp_rts WHERE id_temp = 1");
			$sql_rtu2=mysql_fetch_array($sql_rtu);
			if($jumlah > ($sql_rtu2["stock_rtu"]+$jumlah_tmp)){
			sukses('Data Perbaikan melebihi stok di Database', "?pages=brg_kluar_rtu&act=edit");
			}
			else {

				$recount_rtu= $sql_rtu2["stock_rtu"] + $jumlah_tmp;
				$update_temp=mysql_query("UPDATE tb_temp_rts SET stock_rtu='$recount_rtu' WHERE id_temp='1'");
				//update
				$sql_rtu=mysql_query("SELECT stock_rtu from tb_temp_rts WHERE id_temp = 1");
				$sql_rtu2=mysql_fetch_array($sql_rtu);
				$update_rtu= $sql_rtu2["stock_rtu"] - $jumlah;
	
				$update = mysql_query("UPDATE tb_penjualan_rtu SET id_customer='$id_customer', tgl_jual='$tgl_jual', nota='$nota', jumlah='$jumlah',  satuan='$satuan' WHERE id_penjualan_rtu=$id_penjualan_rtu");
				if(update){
					$update_temp=mysql_query("UPDATE tb_temp_rts SET stock_rtu='$update_rtu' WHERE id_temp='1'");
				}
				if($update_temp){
					sukses('Data berhasil disimpan', '?pages=brg_kluar_rtu');
				}
			}
}else{


			$sql_rtu=mysql_query("SELECT stock_rtu from tb_temp_rts WHERE id_temp = 1");
			$sql_rtu2=mysql_fetch_array($sql_rtu);
			if($jumlah > $sql_rtu2["stock_rtu"]){
			sukses('Data Perbaikan melebihi stok di Database', '?pages=brg_kluar_rtu&act=edit');
			}
			else {
				$update_rtu= $sql_rtu2["stock_rtu"] - $jumlah;	
				$insert = mysql_query("INSERT INTO tb_penjualan_rtu VALUES(NULL, '$id_customer', '$tgl_jual', '$nota', '$jumlah', '$satuan')");
				
				$sql_rtu=mysql_query("SELECT id_penjualan_rtu from tb_penjualan_rtu order by id_penjualan_rtu desc");
				$sql_rtu3=mysql_fetch_array($sql_rtu);
				$id_rtu=$sql_rtu3["id_penjualan_rtu"];
				$nota_id="PU".$id_rtu;
				$update_nota = mysql_query("UPDATE tb_penjualan_rtu set nota='$nota_id' WHERE id_penjualan_rtu=$id_rtu");
				
				if($insert){
					$update2 = mysql_query("UPDATE tb_temp_rts set stock_rtu='$update_rtu' WHERE id_temp='1'");				
				}		
				if($update2){
					sukses('Data berhasil disimpan', '?pages=brg_kluar_rtu');
				}
			}
	}
	
}elseif ($act=='del') {
	# code...
	$id_penjualan_rtu = (int) filter($_GET['id']);

	$sql_cek = mysql_query("SELECT * from tb_penjualan_rtu where id_penjualan_rtu='$id_penjualan_rtu'");
	$cek = mysql_fetch_array($sql_cek);
	$jumlah = $cek['jumlah'];
	

	if ($cek['id_penjualan_rtu'] == true) {
		$sql_temp = mysql_query("SELECT * FROM tb_temp_rts WHERE id_temp = 1 ");
		$j_stok = mysql_fetch_array($sql_temp);
		$jml_stok = $j_stok['stock_rtu'] + $jumlah;
		$sql_update_stok = mysql_query("UPDATE tb_temp_rts SET stock_rtu = $jml_stok WHERE id_temp='1'");

	}

	$delete = mysql_query("DELETE FROM tb_penjualan_rtu WHERE id_penjualan_rtu=$id_penjualan_rtu");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=brg_kluar_rtu');
}

?>					
<script type="text/javascript">
$(document).ready(function () {
	$("#id_customer").chosen();  
})
</script>		